<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\exception\ValidateException;
use service\JsonService as Json;
use think\facade\{View, Session};

use app\common\model\{Notice, Shop};
use app\common\validate\{NoticeValidate};

class NoticeController extends BaseController
{
    /**
     * 渲染公告列表页面
     */
    public function notice_list()
    {   
        return view();
    }

    /**
     * 渲染公告添加页面
     */
    public function notice_add()
    {
        return view();
    }

    /**
     * 渲染公告编辑页面
     */
    public function notice_edit()
    {   
        $data = input('');

        $info = Notice::getFind(['id' => $data['id']]);

        View::assign('info', $info);
        return view();
    }

    /**
     * 获取公告列表数据
     */
    public function notice_data()
    {   
        $data = input('');

        try{
            validate(NoticeValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $token = Session::get('token');

        if($token['role'] == '超级管理员'){
            $where[] = ['shop_id', '=', 0];
        }else{
            $where[] = ['shop_id', '=', $token['id']];
        }

        if($data && isset($data['key'])){
            if(!empty($data['key']['title'])){
                $where[] = ['title', 'like', '%' . $data['key']['title'] . '%'];
            }
        }
        
        $field = 'id, shop_id, title, content, create_time';
        $list = Notice::getList($where, 1, $data['page'], $data['limit'], $field, 'id DESC');

        if($list === false) return Json::fail('暂无数据~');
        return Json::success('ok', $list, Notice::getNum($where));
    }

    /**
     * 添加公告
     */
    public function notice_add_data()
    {   
        $data = input('');

        try{
            validate(NoticeValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $token = Session::get('token');

        if($token['role'] !== '超级管理员'){
            $data['shop_id'] = $token['id'];
        }

        $flag = Notice::add($data);

        if($flag === false) return Json::fail('添加失败');
        return Json::success('添加成功');
    }

    /**
     * 修改公告
     */
    public function notice_edit_data()
    {
        $data = input('');

        try{
            validate(NoticeValidate::class)->scene('edit')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $token = Session::get('token');

        if($token['role'] !== '超级管理员'){
            $data['shop_id'] = $token['id'];
        }

        $flag = Notice::edit($data);

        if($flag === false) return Json::fail('修改失败');
        return Json::success('修改成功');
    }

    /**
     * 删除公告
     */
    public function notice_del()
    {
        $data = input('');

        try{
            validate(NoticeValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $flag = Notice::destroy($data['id']);

        if(!$flag) return Json::fail('删除失败');
        return Json::success('删除成功');
    }

    /**
     * 渲染店铺公告列表页面
     */
    public function notice_shop_list()
    {   
        return view();
    }

    /**
     * 渲染店铺公告添加页面
     */
    public function notice_shop_add()
    {   
        return view();
    }

    /**
     * 渲染店铺公告编辑页面
     */
    public function notice_shop_edit()
    {   
        $data = input('');

        $info = Notice::getFind(['id' => $data['id']]);

        View::assign('info', $info);
        return view();
    }
}
